package org.example;

import org.example.config.AppConfig;
import org.example.service.OrderService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
    public static void main(String[] args) {
        // 使用Java配置方式加载Spring上下文
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        OrderService orderService = context.getBean(OrderService.class);

        System.out.println("add 测试");
        try {
            orderService.add("手机", 2);
        } catch (Exception e) {
            System.out.println("add方法执行异常: " + e.getMessage());
        }

        System.out.println("\nadd 测试");
        try {
            orderService.add("笔记本电脑", 1);
        } catch (Exception e) {
            System.out.println("add方法执行异常: " + e.getMessage());
        }

        System.out.println("\ndeleta 测试");
        try {
            orderService.delete("手机");
        } catch (Exception e) {
            System.out.println("delete方法执行异常: " + e.getMessage());
        }

        System.out.println("\nfind 测试");
        try {
            int count = orderService.find("电脑");
            System.out.println("查询结果: " + count);
        } catch (Exception e) {
            System.out.println("find方法执行异常: " + e.getMessage());
        }

        // 关闭Spring上下文
        ((AnnotationConfigApplicationContext) context).close();
    }
}